package com.hcms.system.service.impl;

import com.hcms.system.dao.AdminRoleDao;
import com.hcms.system.domain.AdminRoleDo;
import com.hcms.system.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {

    @Autowired
    AdminRoleDao adminRoleMapper;

    /**
     * 根据 管理员 Id 获取管理员角色
     *
     * @param adminId 管理员 Id
     */
    @Override
    public AdminRoleDo getByAdmin(int adminId) {
        AdminRoleDo adminRole = adminRoleMapper.getByAdmin(adminId);
        return adminRole;
    }

    /**
     * 根据管理员设置角色信息
     *
     * @param adminId    管理员 Id
     * @param roleId     角色 Id
     * @param updateUser 操作人姓名
     */
    @Override
    public int setRoleByAdmin(int adminId, int roleId, String updateUser) {
        int count = 0;
        if (adminId == 0 || roleId < 1) {
            return count;
        }

        Date date = new Date();
        AdminRoleDo adminRoleDo = adminRoleMapper.getByAdmin(adminId);
        if (adminRoleDo == null) {
            adminRoleDo = new AdminRoleDo();
            adminRoleDo.setCreateTime(date);
            adminRoleDo.setCreateUser(updateUser);
            adminRoleDo.setUpdateTime(date);
            adminRoleDo.setUpdateUser(updateUser);
            adminRoleDo.setMarkStatus(0);
            adminRoleDo.setAdminId(adminId);
            adminRoleDo.setRoleId(roleId);
            count = adminRoleMapper.save(adminRoleDo);
        } else if (adminRoleDo.getRoleId() != roleId) {
            adminRoleDo.setAdminId(adminId);
            adminRoleDo.setRoleId(roleId);
            adminRoleDo.setMarkStatus(0);
            adminRoleDo.setUpdateTime(date);
            adminRoleDo.setUpdateUser(updateUser);
            count = adminRoleMapper.updateByAdmin(adminRoleDo);
        }
        return count;
    }
}
